<template>
	<view>
		<view v-if="!is_login" class="login">
			<!-- <view @click="goMusic">
				<image :src="qr_code" mode="widthFix" style="width: 200rpx;display: block;margin: auto;"></image>
			</view>
			<view class="text-area">
				<text v-if="!status_msg" class="title"> 仅支持扫码登录</text>
				<text v-if="status_msg" class="title">{{status_msg}}</text>
			</view> -->
			<view class="login-box">
				<view>
					<p>账号</p>
					<input v-model="formData.phone" placeholder="请输入手机号" placeholder-style="color:#C7C7C7">
				</view>
				<view style="margin-top: 30rpx;">
					<p>登录密码</p>
					<input v-model="formData.password" placeholder="请输入密码" type="password"
						placeholder-style="color:#C7C7C7">
				</view>

				<view @click="handleLogin">登录</view>
			</view>
		</view>
		<view v-if="is_login" class="profile">
			<view class="profile-top">
				<image :src="profile.avatarUrl"></image>
				<b class="name">{{profile.nickname}}</b>
				<view class="name-bottom">
					<span>{{profile.follows}} 关注</span>
					<span>{{profile.followeds}} 粉丝</span>
					<!-- <span>Lv.{{detail.level}}</span> -->
					<span>性别.{{profile.gender?'男':'女'}}</span>
				</view>
			</view>
			<view class="profile-menu" style="height: 200rpx;">
				<span>生日.{{dayjs(profile.birthday).format('YYYY-MM-DD')}}</span>
			</view>
			<view class="profile-menu">
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getQrKey,
		getStatusApi,
		getLoginStatus,
		getProfile,
		loginCellPhoneApi
	} from "@/api/api.js"
	import dayjs from 'dayjs'
	import {
		myCache
	} from "@/utils.js"
	export default {
		data() {
			return {
				dayjs,
				title: 'Hello',
				qr_code: "",
				status: 0,
				status_msg: "",
				key: "",
				timer: null,
				is_login: false,

				formData: {
					phone: null,
					password: null
				},
				detail: {},
				profile: {}
			}
		},
		onLoad() {
			// this.getQr();
			this.autoLogin()
		},
		watch: {
			status(val) {
				// if(val!==801) this.status_msg = status.data.message;
			}
		},
		methods: {
			async autoLogin() {
				if (myCache('cookie') && myCache('uid')) {
					let detail = await getProfile({
						uid: myCache('uid')
					})
					this.is_login = true;
					this.detail = detail;
					this.profile = detail.profile;
				}
			},
			async handleLogin() {
				try {
					this.$showToast('登录成功');
					this.is_login = true;
					const res = await loginCellPhoneApi(this.formData)
					console.log(res);
					this.profile = res.profile;
					myCache('cookie', res.cookie);
					myCache('uid', res.profile.userId);
				} catch (e) {
					//TODO handle the exception
				}
			},
			async getQr() {
				var timeStamp = new Date().getTime();
				var key = await getQrKey();
				this.key = key.data.unikey;
				let _qr = await this.$http(`/login/qr/create?time=${timeStamp}`, {
					key: this.key,
					qrimg: "base64"
				});
				this.qr_code = _qr.qrimg;
				this.getStatus();
			},
			getStatus() {
				this.timer = setInterval(async () => {
					let data = {
						key: this.key
					};
					let status = await getStatusApi(data);
					this.status = status.data.code;
					if (this.status != 801) this.status_msg = status.data.message;
					if (this.status == 803) {
						this.$showToast(status.data.message);
						this.is_login = true;
						myCache('cookie', status.data.cookie);
					}
				}, 3000);
			},
			async getLogin() {
				if (myCache('cookie')) {
					let cookie = {
						cookie: myCache('cookie')
					};
					let res = await getLoginStatus(cookie);
					myCache('uid', res.profile.userId);
					if (res) {
						this.$nextTick(() => {
							this.is_login = true;
							clearInterval(this.timer);
							this.$forceUpdate();
						});
					} else {
						this.$nextTick(() => {
							this.is_login = false;
							this.getQr();
							this.$forceUpdate();
						});
					}
					let data = {
						uid: myCache('uid')
					};
					let detail = await getProfile(data);
					this.detail = detail;
					this.profile = detail.profile;
					console.log(detail);
				}
				return;
			},
			goMusic() {
				const url = "/pages/find/find";
				this.$navigateTo(url);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		.login-box {
			width: 100vw;
			height: 100vh;

			view {
				padding: 0 50rpx;

				p {
					margin-bottom: 10rpx;
					color: #000000;
					font-weight: Bold;
				}
			}

			view:nth-child(3) {
				position: absolute;
				top: 45vh;
				left: 50%;
				transform: translateX(-50%);
				width: 80vw;
				height: 88rpx;
				background: linear-gradient(-70deg, #ff5500, #d50000);
				border-radius: 44rpx;
				text-align: center;
				line-height: 80rpx;
				color: #FFF;
			}
		}

		.text-area {
			display: flex;
			justify-content: center;
		}

		.title {
			font-size: 36rpx;
			color: #8f8f94;
		}
	}

	.profile {
		width: 100%;
		height: 100vh;

		.profile-top {
			position: relative;
			width: 95%;
			height: 200rpx;
			margin: auto;
			background-color: rgba(217, 217, 217, .3);
			margin-top: 60rpx;
			border-radius: 30rpx;

			image {
				position: absolute;
				top: -60rpx;
				left: 50%;
				transform: translateX(-50%);
				width: 120rpx !important;
				height: 120rpx !important;
				border-radius: 50%;
			}

			.name {
				position: absolute;
				top: 80rpx;
				left: 50%;
				transform: translateX(-50%);
				font-size: 30rpx;
			}

			.name-bottom {
				position: absolute;
				top: 140rpx;
				left: 50%;
				transform: translateX(-50%);
				display: flex;
				justify-content: space-between;

				span {
					display: block;
					text-align: center;
					margin: 0 20rpx;
					color: #8f8f94;
					font-size: 26rpx;
				}
			}
		}

		.profile-menu {
			width: 95%;
			height: 400rpx;
			margin: auto;
			background-color: rgba(217, 217, 217, .3);
			margin-top: 60rpx;
			border-radius: 30rpx;

			display: flex;
			justify-content: space-between;

			span {
				display: block;
				text-align: center;
				margin: 0 20rpx;
				color: #8f8f94;
				font-size: 26rpx;
			}
		}
	}
</style>